﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.ServiceLocation;

namespace ilanver.Core
{
    public static class SafeServiceLocator<TDependency>
    {
        public static TDependency GetService()
        {
            TDependency service;
            try
            {
                service = (TDependency)ServiceLocator.Current.GetService(typeof(TDependency));
            }
            catch (NullReferenceException)
            {
                //TODO Loglanacak
                throw new NullReferenceException("ServiceLocator has not been initialized; " +
                     "I was trying to retrieve " + typeof(TDependency).ToString());
            }
            catch (ActivationException ex)
            {
                //TODO Loglanacak
                throw new ActivationException("The needed dependency of type " + typeof(TDependency).Name +
                   " could not be located with the ServiceLocator. You'll need to register it with " +
                   "the Common Service Locator (CSL) via your IoC's CSL adapter. " + ex.Message, ex);
            }


            return service;
        }
    }
}
